package web.eshop.tree;

import domain.shop.Category;
import org.apache.wicket.Component;
import org.apache.wicket.MarkupContainer;
import org.apache.wicket.PageParameters;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.tree.BaseTree;
import org.apache.wicket.markup.html.tree.LinkIconPanel;
import org.apache.wicket.markup.html.tree.LinkTree;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import web.eshop.MultipleProductsEshopPage;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeModel;

public class BookmarkablePageLinkTree extends LinkTree {
    public BookmarkablePageLinkTree(String id, TreeModel model) {
        super(id, model);
        setLinkType(BaseTree.LinkType.AJAX_FALLBACK);
        getTreeState().expandAll();
        getTreeState().setAllowSelectMultiple(false);
        setRootLess(true);
    }

    @Override
    protected Component newNodeComponent(String id, IModel model) {
        return new LinkIconPanel(id, model, BookmarkablePageLinkTree.this) {
            private static final long serialVersionUID = 1L;

            @Override
            protected void addComponents(final IModel model, final BaseTree tree) {
                final IModel objModel = new LoadableDetachableModel() {

                    @Override
                    protected Object load() {
                        return ((DefaultMutableTreeNode) model.getObject()).getUserObject();
                    }
                };
                BaseTree.ILinkCallback callback = new BaseTree.ILinkCallback() {
                    private static final long serialVersionUID = 1L;

                    public void onClick(AjaxRequestTarget target) {
                        if (target == null) {
                        } else {
                            PageParameters params = new PageParameters();
                            if (objModel.getObject() instanceof String) {
                                target.appendJavascript("window.location=" + BookmarkablePageLinkTree.this.urlFor(
                                        MultipleProductsEshopPage.class, params));
                            } else if (objModel.getObject() instanceof Category) {
                                params.add("0", ((Category) objModel.getObject()).getId().toString());
                                target.appendJavascript("window.location=" + BookmarkablePageLinkTree.this.urlFor(
                                        MultipleProductsEshopPage.class, params));
                            }
                        }
                    }
                };

                MarkupContainer iconContainer = tree.newLink("iconLink", callback);
                iconContainer.add(newImageComponent("icon", tree, model));
                add(iconContainer);

                PageParameters params = new PageParameters();
                if (objModel.getObject() instanceof String) {
                    Link contentLink = new BookmarkablePageLink("contentLink", MultipleProductsEshopPage.class, params);
                    contentLink.add(new Label("content", objModel.getObject().toString())
                    );
                    add(contentLink);
                } else if (objModel.getObject() instanceof Category) {
                    params.add("0", ((Category) objModel.getObject()).getId().toString());
                    Link contentLink = new BookmarkablePageLink("contentLink", MultipleProductsEshopPage.class, params);
                    add(contentLink);
                    contentLink.add(new Label("content", objModel.getObject().toString()));
                }
            }
        };
    }
}